/**
 * @author trex279
 */

$(document).ready(function(){
	window.htmlLoader.navigateInSystemBrowser = true;
	$('#tabs').tabs({
		select: function() {
        	return isLoggedin;
		}
    });
	thisApplication.initialize();
	
	//for login tab
	thisApplication.nativeWin.dispatchEvent(loadingStartEvent);
	document.getElementById("redditPage").contentWindow.parentSandboxBridge = parentsExposedObject;
	
	//for messages tab
	$("#messageRefresh").click(function(event){
		checkMessages();
		thisApplication.nativeWin.dispatchEvent(loadingStartEvent);
	});
	
	//for submissions tab
	$('#submissionRefresh').click(function(){
		thisApplication.nativeWin.dispatchEvent(loadingStartEvent);
		getsubmissions();
	});
	
	//for comments tab
	$('#commentRefresh').click(function(){
		thisApplication.nativeWin.dispatchEvent(loadingStartEvent);
		getComments();
	});
	
	//Timer to update all three tabs regularly
	var timer = window.setInterval( function(){
    	checkMessages();
		getsubmissions();
		getComments(); 
 	}, timeInterval );
	
	var firstTime = {};
	firstTime.message = true;
	firstTime.submission = true;
	firstTime.comment = true;
	
	//for messages tab
	$("#messageSortTab").click(function(event){
		if(isLoggedin)
			if (firstTime.message) {
				checkMessages();
				thisApplication.nativeWin.dispatchEvent(loadingStartEvent);
				firstTime.message = false;
			}
	});
	
	//for submissions tab
	$('#submissionsSort').click(function(){
		if(isLoggedin)
			if (firstTime.submission) {
				thisApplication.nativeWin.dispatchEvent(loadingStartEvent);
				getsubmissions();
				firstTime.submission = false;
			}
	});
	
	//for comments tab
	$('#commentSort').click(function(){
		if(isLoggedin)
			if (firstTime.comment) {
				thisApplication.nativeWin.dispatchEvent(loadingStartEvent);
				getComments();
				firstTime.comment = false;
			}
	});

	
});